﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using IHISCETeim.BLL;
using IHISCETeim.Library;
using IHISCETeim.Model;
using System.Configuration;
using System.Data;
using System.Text;
/*?
 2014-07-24 暂时没用
 */
namespace IHISCETeim.Web.Asmx
{
    /// <summary>
    /// AgentAutoComplete 的摘要说明
    /// </summary>
    public class AgentAutoComplete : IHttpHandler
    {

        public enum ajaxmethod
        {   
            /// <summary>
            /// 获取物料
            /// </summary>
            getoitmlist,



        }

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            string method = context.Request["ajaxmethod"];
            ajaxmethod xmethod = (ajaxmethod)Enum.Parse(typeof(ajaxmethod), method.Trim().ToLower());
            string temp = "";
            switch (xmethod)
            {
                case ajaxmethod.getoitmlist:
                    temp = getoitmlist();
                    break;
            }
            //只支持 jsonp请求
            string callbackfunname = GetRequestValue<string>("callback", "");
            temp = string.IsNullOrEmpty(callbackfunname) ? temp : callbackfunname + "('" + temp + "')";
            context.Response.Write(temp);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        #region 页面参数获取
        protected T GetRequestValue<T>(string name)
        {
            return HttpContext.Current.Request.GetRequestValue<T>(name);
        }

        protected T GetRequestValue<T>(string name, bool useDefValue, T defValue)
        {
            return HttpContext.Current.Request.GetRequestValue<T>(name, useDefValue, defValue);
        }
        protected T GetRequestValue<T>(string name, T defValue)
        {
            return HttpContext.Current.Request.GetRequestValue<T>(name, true, defValue);
        }
        #endregion

        /// <summary>
        /// 查询关键字
        /// </summary>
        protected string key
        {
            get
            {
                return GetRequestValue<string>("key", true, "");
            }
        }
        /// <summary>
        /// 获取记录的前几条
        /// </summary>
        protected int toprow
        {
            get
            {

                return GetRequestValue<int>("limit", true, 20);
            }
        }
        protected string rowSplitTag
        {
            get
            {
                return ";";
            }
        }

        /// <summary>
        /// 物料查询
        /// </summary>
        /// <returns></returns>
        private string getoitmlist()
        {
            string showtype = GetRequestValue<string>("showtype", "102");
            DataTable dt = null;
            if (showtype == "")
            {
                dt = SBOController.GetOITMList(key, toprow);
            }
            if (showtype == "101")
            {
                dt = SBOController.GetYanCaiLiaoOITM(key, toprow);
            }
            if (showtype == "102")
            {
                dt = SBOController.GetPaiJinOITMList(key, toprow);
            }


            if (dt == null)
                return "";

            string callback = "{\"citylist\":[";
            if (!string.IsNullOrEmpty(key))
            {   
                StringBuilder sbJson = new StringBuilder();
                foreach (DataRow DR in dt.Rows)
                {
                    sbJson.Append("{\"itemcode\":\"" + DR["itemcode"].ToString() + "\",\"itemname\":\"" + DR["itemname"].ToString() + "\"},");
                }
                callback += sbJson.ToString().Length > 0 ? sbJson.ToString().TrimEnd(',') : "";

            }
            callback = callback + "]}";
            return callback;
        }
    }
}